package com.cloudsigns.factorymanager2.produce.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.produce.activity.ProduceReceiveOrderItemDetailActivity;
import com.cloudsigns.factorymanager2.produce.adapter.GetProductOrderListAdapter;
import com.cloudsigns.factorymanager2.produce.bean.ProduceProduceOrderBean;
import com.cloudsigns.factorymanager2.produce.utils.ProduceForTeamEvent;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.ArrayList;
import java.util.List;

import de.greenrobot.event.EventBus;

public class ProduceReceiveOrderWaitingFragment extends Fragment {
    private final String TAG = ProduceReceiveOrderWaitingFragment.class.getSimpleName();
    private ListView lv;
    private GetProductOrderListAdapter adapter;
    private List<ProduceProduceOrderBean> produceOrderList = new ArrayList<>();

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_produce_receive_order_waiting, null);
        lv = view.findViewById(R.id.lv);

        adapter = new GetProductOrderListAdapter(getActivity(), TAG);
        adapter.setData(produceOrderList);
        lv.setAdapter(adapter);

        lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
                ProduceProduceOrderBean produceOrderBean = produceOrderList.get(position);
                Intent mIntent = new Intent(getActivity(), ProduceReceiveOrderItemDetailActivity.class);
                mIntent.putExtra(ConstantUtil.KEY_1, produceOrderBean);
                mIntent.putExtra(ConstantUtil.KEY_2,"ProduceReceiveOrderWaitingFragment");
                startActivityForResult(mIntent, ConstantUtil.START_ACTIVITY_KEY_1);
            }
        });
        return view;
    }

    public void initData(List<ProduceProduceOrderBean> produceOrderBeanList) {
        if (produceOrderBeanList != null) {
            produceOrderList.clear();
            produceOrderList.addAll(produceOrderBeanList);
            adapter.setData(produceOrderList);
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (null != data) {
            switch (requestCode) {
                case ConstantUtil.START_ACTIVITY_KEY_1:
                    EventBus.getDefault().post(new ProduceForTeamEvent());
                    break;
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
